﻿using System;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;

namespace gKodes.Windows.Forms
{
    public class CellPropertiesConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context,
            Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context,
            CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return ((CellProperties)value).ToString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, 
            Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, 
            CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] splits = value.ToString().Split('/', ',', '.');
                switch(splits.Length){
                    case 6:
                        return new CellProperties(
                            new Size(int.Parse(splits[0]), int.Parse(splits[1])),
                            new Padding(int.Parse(splits[2]), int.Parse(splits[3]),
                                int.Parse(splits[4]), int.Parse(splits[5])));
                    case 5:
                        return new CellProperties(
                            new Size(int.Parse(splits[0]), 1),
                            new Padding(int.Parse(splits[1]), int.Parse(splits[2]),
                                int.Parse(splits[3]), int.Parse(splits[4])));
                }
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
            object value, Attribute[] attributes)
        {
            return new PropertyDescriptorCollection(mPPropertyDescriptors, true);
        }

        #region PropertyDescriptor & Static Constructor

        protected class CellPPropertyDescriptor : SimplePropertyDescriptor
        {
            public CellPPropertyDescriptor(string name, Type propertyType)
                : base(typeof(CellProperties), name, propertyType) { }

            public CellPPropertyDescriptor(string name, Type propertyType, Attribute[] attributes)
                : base(typeof(CellProperties), name, propertyType, attributes) { }

            public override bool IsReadOnly
            {
                get { return true; }
            }

            public override object GetValue(object component)
            {
                if (this.Name.Equals("Size"))
                {
                    return ((CellProperties)component).Size;
                }
                else if (this.Name.Equals("Padding"))
                {
                    return ((CellProperties)component).Padding;
                }
                else if (this.Name.Equals("Dividend"))
                {
                    return ((CellProperties)component).Dividend;
                }
                throw new NotImplementedException();
            }

            public override void SetValue(object component, object value)
            {
                throw new NotImplementedException();
            }
        }

        private static CellPPropertyDescriptor[] mPPropertyDescriptors
            = new CellPPropertyDescriptor[4];

        static CellPropertiesConverter()
        {
            mPPropertyDescriptors[3] = new CellPPropertyDescriptor("Size", typeof(Size));
            mPPropertyDescriptors[2] = new CellPPropertyDescriptor("Padding", typeof(Padding));
            mPPropertyDescriptors[1] = new CellPPropertyDescriptor("Segment", typeof(int));
            mPPropertyDescriptors[0] = new CellPPropertyDescriptor("Dividend", typeof(int));
        }

        #endregion
    }
}
